package pers.dawnyang.modular.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import pers.dawnyang.common.constant.Const;
import pers.dawnyang.common.domain.TreeNode;
import pers.dawnyang.common.domain.entity.SysMenu;
import pers.dawnyang.common.domain.entity.SysUser;
import pers.dawnyang.common.util.math.RandomUtil;
import pers.dawnyang.modular.system.domain.common.DeptInfo;
import pers.dawnyang.modular.system.domain.common.PermsInfo;
import pers.dawnyang.modular.system.domain.common.RoleInfo;
import pers.dawnyang.modular.system.domain.common.UserInfo;
import pers.dawnyang.modular.system.domain.request.system.EditPwdReq;
import pers.dawnyang.modular.system.mapper.SysMenuMapper;
import pers.dawnyang.modular.system.mapper.SysUserMapper;
import pers.dawnyang.modular.system.mapper.SystemMapper;
import pers.dawnyang.modular.system.service.SystemService;

/**
 * @author dawn
 * @date 2021/02/08
 */
@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SystemMapper systemMapper;

    @Override
    public List<TreeNode> getSideNav(Map<String, String> map, List<String> permsList) {
        String id = map.get("id");
        if (StringUtils.isBlank(id)) {
            id = "0";
        }
        List<TreeNode> treeNodeList = new ArrayList<>();
        List<SysMenu> menuList = sysMenuMapper.getSideNav(id, permsList);
        for (SysMenu menu : menuList) {
            TreeNode treeNode = new TreeNode();
            treeNode.setId(menu.getMenuId() + "");
            treeNode.setText(menu.getMenuName());
            treeNode.setHref(menu.getUrl());
            treeNode.setIcon(menu.getIcon());
            if (menu.getMenuType() == 0) {
                treeNode.setHasChildren("1");
            } else {
                treeNode.setHasChildren("0");
            }
            treeNodeList.add(treeNode);
        }
        return treeNodeList;
    }

    @Override
    public UserInfo getUserInfo(String userCode) {
        UserInfo userInfo = systemMapper.getUserInfo(userCode);
        Long userId = userInfo.getUserId();
        List<RoleInfo> roleList = systemMapper.getRoleList(userId);
        for (RoleInfo roleInfo : roleList) {
            List<PermsInfo> permsInfoList = systemMapper.getPermsList(roleInfo.getRoleId());
            roleInfo.setPermsList(permsInfoList);
        }
        List<DeptInfo> deptList = systemMapper.getDeptList(userId);
        userInfo.setRoleList(roleList);
        userInfo.setDeptList(deptList);
        return userInfo;
    }

    @Override
    public boolean checkOldPwd(String oldPwd, String userCode) {
        LambdaQueryWrapper<SysUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysUser::getUserCode, userCode);
        SysUser user = sysUserMapper.selectOne(lqw);
        String oldPwdMd5Hash = new Md5Hash(oldPwd, user.getSalt(), Const.MD5_NUM).toString();
        if (oldPwdMd5Hash.equals(user.getPassword())) {
            return true;
        }
        return false;
    }

    @Override
    public void editPwd(EditPwdReq req, String userCode) {
        String salt = RandomUtil.getRandomString(Const.SALT_FIGURE);
        String newPwdMd5Hash = new Md5Hash(req.getNewPwd(), salt, Const.MD5_NUM).toString();
        SysUser user = new SysUser();
        user.setPassword(newPwdMd5Hash);
        user.setSalt(salt);
        LambdaUpdateWrapper<SysUser> luw = new LambdaUpdateWrapper<>();
        luw.eq(SysUser::getUserCode, userCode);
        sysUserMapper.update(user, luw);
    }

}
